﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace DJH.School.Common
{
    public class PaginatedList<T> : List<T>
    {
        public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            TotalPages = (int) Math.Ceiling(count / (decimal) pageSize);
            this.AddRange(items);
        }

        /// <summary>
        ///     当前页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        ///     总页数
        /// </summary>
        public int TotalPages { get; set; }

        /// <summary>
        ///     判断是否有上一页
        /// </summary>
        public bool HasLastPage => PageIndex > 1;

        /// <summary>
        ///     判断是否有下一页
        /// </summary>
        public bool HasNextPage => PageIndex < TotalPages;

        /// <summary>
        ///     创建分页
        /// </summary>
        /// <param name="source"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static async Task<PaginatedList<T>> CreatePagingAsync(IQueryable<T> source, int pageIndex, int pageSize)
        {
            var count = await source.CountAsync();
            var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            var dtos = new PaginatedList<T>(items, count, pageIndex, pageSize);
            return dtos;
        }
    }
}